package com.nondo.dean.community.controller.api.app;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.AliyunOssTool;
import com.nondo.dean.common.tool.StringUtils;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.community.domain.Article;
import com.nondo.dean.community.model.result.CategoryArticleResult;
import com.nondo.dean.community.service.ArticleService;

/**
 * @author ken.
 * @version 1.0
 * @email 695093513@qq.com
 * @date 2018/3/28
 */
@RestController("apiAppCommunityArticle")
@RequestMapping("/{v}/app/article")
public class ArticleController {
    @Autowired
    ArticleService articleService;

    @Autowired
    AliyunOssTool aliyunOssTool;

    /**
     * 列表
     *
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<Article>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<Article> query) {
        View<Page<Article>> view = new View<Page<Article>>(20000, "获取数据成功");

        Article article = query.getData();

        Page<Article> page = new Page<Article>(query.getPage(), query.getLimit(), articleService.count(article));

        List<Article> gradeList = articleService.getList(query.getOffset(), query.getLimit(), article);

        page.setContent(gradeList);

        view.setData(page);

        return view;
    }

    /**
     * 获取单个文章信息
     *
     * @param request
     * @param response
     * @param articleId
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/info/{articleId}")
    public View<Article> info(HttpServletRequest request, HttpServletResponse response, @PathVariable int articleId) {
        View view = new View(20000, "获取成功");

        Article article = this.articleService.select(articleId);

        //主图处理
        if (StringUtils.isNoneEmpty(article.getImage())) {
            article.setImage(this.aliyunOssTool.getFileUrl(article.getImage()));
        }

        view.setData(article);

        return view;
    }

    /**
     * 商学院首页
     *
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/home/list")
    public View<Page<CategoryArticleResult>> homeList(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<Article> query) {
        View<Page<CategoryArticleResult>> view = new View<Page<CategoryArticleResult>>(20000, "获取数据成功");

        Article article = query.getData();
        if(article == null){
            article = new Article();
        }
        Page<CategoryArticleResult> page = new Page<CategoryArticleResult>(query.getPage(), query.getLimit(), articleService.count(article));

        List<CategoryArticleResult> result = articleService.commercialCollegeList(query.getOffset(), query.getLimit(), article.getCategoryId());

        page.setContent(result);

        view.setData(page);

        return view;
    }
}
